﻿using OF.BLL.Dish;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OF.Common;
namespace OF.Web.user
{
    public partial class orderdetail : BuyerAdminPage
    {
        BLL.Order.Order bll_order = new BLL.Order.Order();
        BLL.Dish.DishAdmin bll_dish = new BLL.Dish.DishAdmin();
        string orderid = "0", state = "0";
        public Model.Orders model_order;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                orderid = Request.QueryString["orderid"];
                if (string.IsNullOrWhiteSpace(orderid))
                    return;
                Bind();
            }
        }

        public void Bind()
        {
            model_order = bll_order.GetModel(Convert.ToInt32(orderid));
            if (model_order == null) return;

            if (model_order.state == (int)Enmus.ORDER_STATE.CONFIRM_SUC)
            {
                divbtn.Visible = true;
            }
            else
            {
                divbtn.Visible = false;
            }
            hforderid.Value = model_order.id.ToString();
            hfstate.Value = model_order.state.ToString();

            labordersn.Text = model_order.ordersn;
            labtype.Text = model_order.ordertype == 1 ? "外卖" : "点餐";

            labtime.Text = model_order.scheduledtime;
            labphone.Text = model_order.phone;
            if (model_order.ordertype == 1)
            {

                traddress.Visible = true;
                trcount.Visible = false;
            }
            else
            {
                traddress.Visible = false;
                trcount.Visible = true;
            }
            labaddress.Text = model_order.sendaddress;
            labcount.Text = model_order.population == null ? "" : model_order.population.ToString();
            labremark.Text = model_order.remark;

            rptList.DataSource = bll_order.GetOrderDetailsById(Convert.ToInt32(orderid));
            rptList.DataBind();
        }


        public string GetName(object dishid)
        {
            if (dishid != null)
            {
                Model.Dish model = bll_dish.GetModel(Convert.ToInt32(dishid));
                if (model != null)
                    return model.dishname;
            }
            return string.Empty;

        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            int state = Convert.ToInt32(hfstate.Value);
            if (bll_order.Update(hforderid.Value, (int)Enmus.ORDER_STATE.COMPLETE))
            {
                //插入日志
                bll_order.AddOrderLog(state, (int)Enmus.ORDER_STATE.CONFIRM_SUC, Convert.ToInt32(hforderid.Value), 2, WebUtils.GetBuyerId());

                OF.Common.MessageBox.ShowAndRedirect(this, "处理订单成功", "orderlist.aspx");
            }
            else
            {
                OF.Common.MessageBox.Show(this, "操作失败");
            }
        }

        protected void btnNo_Click(object sender, EventArgs e)
        {
            int state = Convert.ToInt32(hfstate.Value);
            if (bll_order.Update(hforderid.Value, (int)Enmus.ORDER_STATE.CANCEL))
            {
              
                //插入日志
                bll_order.AddOrderLog(state, (int)Enmus.ORDER_STATE.CONFIRM_Fail, Convert.ToInt32(hforderid.Value), 2, WebUtils.GetBuyerId());

                OF.Common.MessageBox.ShowAndRedirect(this, "该订单已取消!", "orderlist.aspx");
            }
            else
            {
                OF.Common.MessageBox.Show(this, "操作失败");
            }
        }
    }
}